using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using Gavel.Common.Component.Helper;
using Gavel.Common.Component.ViewModel;
using Gavel.Common.Utilities.Const;

namespace Gavel.Common.Component.Resource;

/// <summary>
/// 通知子项
/// </summary>
public partial class NotificationItem
{
    /// <summary>
    /// 默认构造
    /// </summary>
    public NotificationItem()
    {
        InitializeComponent();
    }

    /// <summary>
    /// 带参构造
    /// </summary>
    /// <param name="type">通知类型</param>
    /// <param name="message">通知消息</param>
    public NotificationItem(NotifyType type, string message)
    {
        InitializeComponent();
        NotificationViewModel viewModel = new NotificationViewModel
        {
            Type = type,
            Message = message
        };

        DataContext = viewModel;
    }

    /// <summary>
    /// 消息关闭计时器
    /// </summary>
    private readonly DispatcherTimer _closeTimer = new() { Interval = new TimeSpan(0, 0, 5) };

    /// <summary>
    /// 删除消息事件
    /// </summary>
    /// <param name="sender">事件发送者</param>
    /// <param name="e">事件参数</param>
    private void RemoveMessage(object sender, RoutedEventArgs e) => NotifyHelper.Instance.RemoveNotification(this);

    /// <summary>
    /// 显示动画
    /// </summary>
    public void AppearAnimation()
    {
        TranslateTransform translate = new TranslateTransform(320, 0);
        RenderTransform = translate;
        DoubleAnimation animation = new DoubleAnimation
        {
            To = 0,
            Duration = TimeSpan.FromSeconds(0.5),
            EasingFunction = new ElasticEase { Oscillations = 2, Springiness = 4 }
        };

        translate.BeginAnimation(TranslateTransform.XProperty, animation);
    }

    /// <summary>
    /// 消失动画
    /// </summary>
    /// <param name="callback">消失动画回调</param>
    public void DisappearAnimation(Action callback)
    {
        DoubleAnimation animation = new DoubleAnimation
        {
            To = 320,
            Duration = TimeSpan.FromSeconds(0.3),
            EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }
        };

        animation.Completed += (_, _) => callback.Invoke();
        RenderTransform.BeginAnimation(TranslateTransform.XProperty, animation);
    }

    /// <summary>
    /// 自动关闭
    /// </summary>
    public void AutoClose()
    {
        _closeTimer.Tick += (_, _) =>
        {
            _closeTimer.Stop();
            NotifyHelper.Instance.RemoveNotification(this);
        };

        _closeTimer.Start();
    }

    /// <summary>
    /// 重写鼠标悬浮事件
    /// 鼠标悬浮时停止自动关闭
    /// </summary>
    /// <param name="e">鼠标悬浮事件参数</param>
    protected override void OnMouseEnter(MouseEventArgs e)
    {
        base.OnMouseEnter(e);
        _closeTimer.Stop();
    }

    /// <summary>
    /// 重写鼠标离开事件
    /// 鼠标离开时开始自动关闭
    /// </summary>
    /// <param name="e">鼠标离开事件参数</param>
    protected override void OnMouseLeave(MouseEventArgs e)
    {
        base.OnMouseLeave(e);
        _closeTimer.Start();
    }
}